package com.example.qxfw.slhx.controller;


import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.qx.SatelliteNephoram3;
import com.example.qxfw.mongodb.qx.entity.QxYtFile;
import com.example.qxfw.mongodb.qx.service.QxYtFileService;
import com.example.qxfw.slhx.service.HydfxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@ApiIgnore
@Api(tags = "森林火险--火源点查询")
@CrossOrigin
@RestController
@RequestMapping("/FireSourcePointQueryController")
public class FireSourcePointQueryController {
    @Autowired
    private HydfxService hydfxService;
    @Autowired
    private QxYtFileService qxYtFileService;

    @ApiOperation("森林火险--火源点查询-查询")
    @GetMapping("/getAllOfSatelliteNephoram3")
    public SysResult getAllOfSatellitenephoram3(String startDatechar, String endDatechar) {
        List<SatelliteNephoram3> list = hydfxService.getAllOfSatelliteNephoram3(startDatechar, endDatechar);
        return SysResult.success(list);
    }

    @ApiOperation("森林火险--火源点查询-详细信息")
    @GetMapping("/getByDateCharAndDataYear")
    public void selectById(String dateChar, String timeChar, String yttype, HttpServletResponse response) throws IOException {
        System.out.println(dateChar);
        System.out.println(timeChar);
        System.out.println(yttype);
        QxYtFile qxYtFile = qxYtFileService.selectByDateCharTimeChatYtType1(yttype, dateChar, timeChar);
        if (qxYtFile != null) {
            InputStream inputStream = new ByteArrayInputStream(qxYtFile.getData());
            response.setContentType("application/octet-stream");
            response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("file", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] b = new byte[1024];
            int len;
            while ((len = inputStream.read(b)) > 0) {
                outputStream.write(b, 0, len);
            }
            inputStream.close();

        } else {
            System.out.println("文件不存在！");
        }
    }
}
